package com.hualu.xmlCode.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.tnc.dao.hibernate.GenericDaoHibernate;

import com.hualu.xmlCode.dao.VideoDetailViewDao;
import com.hualu.xmlCode.model.VideoDetailView;
import com.hualu.xmlCode.model.VideoDetailViewSearchCondition;

public class VideoDetailViewDaoHibernate extends GenericDaoHibernate<VideoDetailView, Integer> implements VideoDetailViewDao {

    public VideoDetailViewDaoHibernate() {
        super(VideoDetailView.class);
    }

    @SuppressWarnings("unchecked")
    public List<VideoDetailView> getVideoDetailViewList() {
        List<VideoDetailView> list = getHibernateTemplate().find("from VideoDetailView");
        if (list == null || list.isEmpty()) {
            return new ArrayList<VideoDetailView>();
        } else {
            return list;
        }
    }

    public List<VideoDetailView> getVideoDetailViewListByCondition(VideoDetailViewSearchCondition videoDetailViewSearchCondition) {

        DetachedCriteria c = DetachedCriteria.forClass(VideoDetailView.class);
        List<VideoDetailView> list = new ArrayList<VideoDetailView>();

        // 输出id
        if (videoDetailViewSearchCondition.getOutputId() != null) {
            c.add(Restrictions.eq("outputId", videoDetailViewSearchCondition.getOutputId()));
        }

        // 源id
        if (videoDetailViewSearchCondition.getSourceId() != null) {
            c.add(Restrictions.eq("sourceId", videoDetailViewSearchCondition.getSourceId()));
        }

        // 高清，标清
        if (StringUtils.isNotBlank(videoDetailViewSearchCondition.getCodeRate())) {
            if (videoDetailViewSearchCondition.getCodeRate().equals("1080")) {
                c.add(Restrictions.or(Restrictions.eq("bitsType", 1), Restrictions.eq("bitsType", 6)));
            } else {
                c.add(Restrictions.or(Restrictions.eq("bitsType", 2), Restrictions.eq("bitsType", 7)));
            }
        }

        // 视频名称模糊查询
        if (StringUtils.isNotBlank(videoDetailViewSearchCondition.getVideoName())) {
            c.add(Restrictions.like("videoName", "%" + videoDetailViewSearchCondition.getVideoName() + "%"));

        }

        list = this.findByCriteria(c);

        return list;
    }

    @SuppressWarnings("unchecked")
    public List<VideoDetailView> findByCriteria(final DetachedCriteria detachedCriteria) {
        return (List) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = detachedCriteria.getExecutableCriteria(session);
                return criteria.list();
            }
        });
    }

}
